package wang.aweb.childcar.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wang.aweb.childcar.filter.UserFilter;
import wang.aweb.childcar.model.dao.MoneyOrderMapper;
import wang.aweb.childcar.model.pojo.Agent;
import wang.aweb.childcar.model.pojo.Member;
import wang.aweb.childcar.model.pojo.MoneyMeal;
import wang.aweb.childcar.model.pojo.MoneyOrder;
import wang.aweb.childcar.model.query.CzOrderParam;
import wang.aweb.childcar.model.vo.AgentVO;
import wang.aweb.childcar.model.vo.MemberVO;
import wang.aweb.childcar.model.vo.MoneyMealVO;
import wang.aweb.childcar.model.vo.MoneyOrderVO;
import wang.aweb.childcar.service.AgentService;
import wang.aweb.childcar.service.CzOrderService;
import wang.aweb.childcar.service.MemberService;
import wang.aweb.childcar.util.MonthDataUtils;
import wang.aweb.childcar.util.StrUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CzOrderServiceImpl implements CzOrderService {
    @Autowired
    private MemberService memberService;
    @Autowired
    private AgentService agentService;
    @Autowired
    private MoneyOrderMapper moneyOrderMapper;
    @Override
    public PageInfo getList(Integer page, Integer limit, CzOrderParam czOrderParam){
        PageHelper.startPage(page,limit);

        List<MoneyOrder> moneyOrderList=moneyOrderMapper.selectList(czOrderParam);
        List<MoneyOrderVO> moneyOrderVOList=moneyOrderListToMoneyOrderVOList(moneyOrderList);

        PageInfo pageInfo=new PageInfo<>(moneyOrderList);
        pageInfo.setList(moneyOrderVOList);
        return pageInfo;
    }
    public List<MoneyOrderVO> moneyOrderListToMoneyOrderVOList(List<MoneyOrder> moneyOrderList){
        List<MoneyOrderVO> moneyOrderVOList=new ArrayList<>();
        for (int i = 0; i < moneyOrderList.size(); i++) {
            MoneyOrder moneyOrder =  moneyOrderList.get(i);
            moneyOrderVOList.add(getMoneyOrderVO(moneyOrder));
        }
        return moneyOrderVOList;
    }
    @Override
    public Integer getMoneyOrderCount(){
        Map<String, Date> query=new HashMap<>();
        query.put("startTime",null);
        query.put("EndTime",null);
        Integer count=moneyOrderMapper.getCount(query);
        return count;
    }
    @Override
    public Integer getMoneyOrderMonthCount(){
        Map<String, Date> query=new HashMap<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String date=sdf.format(new Date());
        SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            query.put("startTime", sdf1.parse(MonthDataUtils.getMonthBegin(date)));
            query.put("endTime",sdf1.parse(MonthDataUtils.getMonthEnd(date)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Integer count=moneyOrderMapper.getCount(query);
        return count;
    }
    @Override
    public Integer getMoneyOrderDayCount(){
        Map<String, Date> query=new HashMap<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        String date=sdf.format(new Date());
        query.put("startTime", MonthDataUtils.getDayStart(date));
        query.put("endTime",MonthDataUtils.getDayEnd(date));
        Integer count=moneyOrderMapper.getCount(query);
        return count;
    }

    @Override
    public String add(MoneyMeal moneyMeal,Member member) {
        String orderNo=StrUtils.getOrderNo();
        MoneyOrder moneyOrder=new MoneyOrder();
        moneyOrder.setOrderno(orderNo);
        moneyOrder.setMid(member.getId());
        moneyOrder.setPrice(moneyMeal.getPrice());
        moneyOrder.setRealprice(moneyMeal.getRealprice());
        moneyOrder.setCreateTime(new Date());
        moneyOrder.setStatus(1);
        moneyOrder.setAgentId(member.getAgentId());
        moneyOrderMapper.insert(moneyOrder);
        return orderNo;
    }
    @Override
    public MoneyOrder selectVOByOrderNo(String orderNo){
        MoneyOrder moneyOrder=moneyOrderMapper.selectByOrderNo(orderNo);
        return moneyOrder;
    }
    @Override
    public void pay(String orderNo) {
        MoneyOrder moneyOrder=selectVOByOrderNo(orderNo);
        if(moneyOrder!=null && moneyOrder.getStatus()==1){
            //只处理未支付的，已支付的不用去管
            //修改订单状态
            moneyOrder.setStatus(2);
            moneyOrderMapper.updateByPrimaryKeySelective(moneyOrder);
            //充值；
            Integer addMoney=moneyOrder.getRealprice();
            Integer memberId=moneyOrder.getMid();
            memberService.chongzhi(addMoney,memberId);

        }
    }

    public MoneyOrderVO getMoneyOrderVO(MoneyOrder moneyOrder){
        if(moneyOrder==null){
            return null;
        }else{
            MoneyOrderVO moneyOrderVO=new MoneyOrderVO();
            BeanUtils.copyProperties(moneyOrder,moneyOrderVO);
            moneyOrderVO.setCreate_time(moneyOrder.getCreateTime());
            MemberVO member=memberService.selectVOById(moneyOrder.getMid());
            AgentVO agent=agentService.findVOById(moneyOrder.getAgentId());
            moneyOrderVO.setAgentinfo(agent);
            moneyOrderVO.setUserinfo(member);

            moneyOrderVO.setCreate_time(moneyOrder.getCreateTime());
            moneyOrderVO.setPrice(Float.valueOf((float)moneyOrder.getPrice()/100));
            moneyOrderVO.setRealprice(Float.valueOf((float)moneyOrder.getRealprice()/100));
            return moneyOrderVO;
        }
    }

}
